from typing import *


class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        size = len(nums)
        ans = [-1] * size
        s = []
        for i in range(size << 1):
            num = nums[i % size]
            while len(s) > 0 and nums[s[-1]] < num:
                ans[s.pop()] = num
            s.append(i % size)
        return ans


class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        size = len(nums)
        ans = [-1] * size
        s = []
        for i in range(size << 1):
            num = nums[i % size]
            while s and nums[s[-1]] < num:
                ans[s.pop()] = num
            if i < size:
                s.append(i)
        return ans
